package com.lanou.lenanapp.adapter;

import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.lanou.lenanapp.R;
import com.lanou.lenanapp.beans.ContributionReview;
import com.lanou.lenanapp.view.CircleImage;
import com.lanou.lenanapp.volley.VolleySingleton;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by dllo on 15/10/24.
 */
public class ContributionReviewAdapter extends RecyclerView.Adapter<ContributionReviewAdapter.ContributionReviewViewHolder>{

    private List<ContributionReview> reviews;
    VolleySingleton volleySingleton = VolleySingleton.getInstance();


    public void addData(List<ContributionReview> datas){
        reviews = datas;
        notifyDataSetChanged();
    }


    class ContributionReviewViewHolder extends RecyclerView.ViewHolder{

        private TextView review_contribution_author_name, review_contribution_created_time,
                review_contribution_content, review_contribution_likes;
        private CircleImage review_contribution_author_avatar;


        public ContributionReviewViewHolder(View itemView) {
            super(itemView);
            review_contribution_author_name = (TextView) itemView.findViewById(R.id.review_contribution_author_name);
            review_contribution_created_time = (TextView) itemView.findViewById(R.id.review_contribution_created_time);
            review_contribution_content = (TextView) itemView.findViewById(R.id.review_contribution_content);
            review_contribution_likes = (TextView) itemView.findViewById(R.id.review_contribution_likes);
            review_contribution_author_avatar = (CircleImage) itemView.findViewById(R.id.review_contribution_author_avatar);

        }
    }

    @Override
    public ContributionReviewViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.activity_review_contribution_gut, null);

        return new ContributionReviewViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ContributionReviewViewHolder holder, int position) {
        if (reviews != null && reviews.size() > 0) {
            ContributionReview con = reviews.get(position);
            holder.review_contribution_author_name.setText(con.getAuthor_name());
            holder.review_contribution_content.setText(con.getContent());

            String time = getNormalTime(con.getCreated_time());
            holder.review_contribution_created_time.setText(time);

            double d = Double.parseDouble(con.getLikes());
            int t = (int) d;
            holder.review_contribution_likes.setText(t + "");

            String url = con.getAuthor_avatar();
            getBitMap(url, holder.review_contribution_author_avatar);

        }

    }

    private void getBitMap(String url, final ImageView imageView) {
        ImageRequest request = new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);
            }
        }, 1000, 1000, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        volleySingleton.addRequest(request);
    }

    public  String  getNormalTime( String value) {
        Long l = Long.valueOf(value);
        l = l * 1000;
        Date date = new Date(l);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
        String time = format.format(date) ;
        return time;
    }


    @Override
    public int getItemCount() {
        return reviews != null && reviews.size() > 0 ? reviews.size() : 0;
    }



}
